package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.burgeon.backend.SHOPBACKEND.entity.vip.Employee;
import com.burgeon.backend.SHOPBACKEND.entity.vip.Vip;
import com.burgeon.backend.SHOPBACKEND.rest.weixin.*;
import com.burgeon.backend.SHOPBACKEND.service.VipService;
import com.burgeon.backend.SHOPBACKEND.service.ex.UploadException;
import com.burgeon.backend.Util.menu.WeixinUtil;
import com.burgeon.backend.service.WechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VipServiceImpl implements VipService {
    @Autowired
    private WechatService wechatService;

    public String getVipByPhone(String phone, String name, String sex, String birth,String openId,String daoId) {

        //根据openId，看是否有VIP信息
        //FindVipByOpenId findVipByOpenId = new FindVipByOpenId("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        //从erp查询是否有vip信息
        VipRest vipRest = new VipRest("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
//        try {
//            Vip vip = findVipByOpenId.getData(openId);
//            //是否有openid查询的VIP信息
//            if(vip==null){
                try {
                  //  System.err.println(phone);
                    Vip vipByP = vipRest.getData(phone);
                   // System.err.println("是否有VIP信息"+vipByP);
                    //根据手机号有VIP的信息
                    if(vipByP!=null){
                        if("null".equals(vipByP.getOpenId())) {
                            System.err.println("没有openid");
                            //修改VIP档案的openid,添加
                            ModRETAILITEM modRETAILITEM = new ModRETAILITEM("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
                            String code = modRETAILITEM.getData(vipByP.getId(),openId);

                            return code;
                        }else if ("null".equals(vipByP.getEmployeeName())){
                            //如果是空的话插入信息到erp
                            findEmployeeById findEmployeeById = new findEmployeeById("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
                            //导购id,查询经销商和所属店仓
                            Employee employee = findEmployeeById.getData(daoId);
                            //修改VIP档案的开卡人,添加
                            ModVipEmp modVipEmp = new ModVipEmp("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
                            String code = modVipEmp.getData(vipByP.getId(),employee.getName());
                            return code;
                        }
                    }else {
                        //如果是空的话插入信息到erp
                        findEmployeeById findEmployeeById = new findEmployeeById("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
                        //导购id,查询经销商和所属店仓
                        Employee employee = findEmployeeById.getData(daoId);

                        //新增VIP
                        InsertVip insertVip = new InsertVip("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
                        String code = insertVip.getData(phone,name,sex,birth,employee,openId);
                        return code;
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }

                return null;
    }

    //根据openId查询VIP信息
    public Vip findVipByOpenId(String openId,String daoId){

        FindVipByOpenId findVipByOpenId = new FindVipByOpenId("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        findEmployeeById findEmployeeById = new findEmployeeById("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            Vip vip =findVipByOpenId.getData(openId);
//            if(vip!=null) {
//                if (null != vip.getEmployeeName()) {
//                    Employee employee = findEmployeeById.getData(daoId);
//                    if(!vip.getEmployeeName().equals(employee.getName())){
//                        throw new UploadException("与您导购不是同一导购");
//
//                    }else {
//                        return vip;
//                    }
//
//                }
//            }
           // System.err.println("根据有数据"+vip);
            return vip;
        } catch (Exception e) {
            e.printStackTrace();
        }



        return null;
    }

    @Override
    public Employee FindEmployNameFindEmployName(String daoId) {
        findEmployeeById findEmployeeById = new findEmployeeById("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            Employee employee = findEmployeeById.getData(daoId);
            return employee;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 累计关注会员数
     * @param account
     * @return
     */
    @Override
    public String getWxVipCount(String account) {
        String accessToken = wechatService.GetAccesTokenByAccount(account);
        String action = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken;
        //发送给微信
        JSONObject jsonObject = WeixinUtil.httpRequest(action,"GET","");
        String count=jsonObject.getString("total");

        return count;
    }


}
